package com.zwr.sheller.shop.service.impl;

import com.zwr.sheller.core.common.LSYBusinessException;
import com.zwr.sheller.core.common.LSYExceptionCode;
import com.zwr.sheller.core.pojo.Merchant;
import com.zwr.sheller.core.util.JsonUtil;
import com.zwr.sheller.core.util.OSSUtil;
import com.zwr.sheller.core.util.ThreadLocalUtil;
import com.zwr.sheller.shop.mapper.MerchantMapper;
import com.zwr.sheller.shop.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author LSY
 * @Date 2022/1/10 11:41
 * @Version 1.0
 **/
@Service
public class MerchantServiceImpl implements MerchantService {

    MerchantMapper merchantMapper;
    @Autowired(required = false)
    public MerchantServiceImpl(MerchantMapper merchantMapper) {
        this.merchantMapper = merchantMapper;
    }

    @Override
    public Merchant findMerchant() {
        temporaryThreadLocal();

        return merchantMapper.selectByPrimaryKey(Long.parseLong(String.valueOf(JsonUtil.stringToObject(String.valueOf(ThreadLocalUtil.getObject()), Map.class).get("merchantId"))));
    }

    @Override
    public void updateMerchant(Merchant merchant, Integer businessLicenseStatus, Integer tradeLicenseStatus, Integer idCardFrontStatus, Integer idCardBackStatus) throws LSYBusinessException {
        temporaryThreadLocal();
        Long merchantId = Long.parseLong(String.valueOf(JsonUtil.stringToObject(String.valueOf(ThreadLocalUtil.getObject()), Map.class).get("merchantId")));

        Merchant originalMerchant = merchantMapper.selectByPrimaryKey(merchantId);

        Example example = new Example(Merchant.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("merchantPhone", merchant.getMerchantPhone());
        List<Merchant> oldMerchant = merchantMapper.selectByExample(example);

        if(oldMerchant.size() != 0 && !merchantId.equals(oldMerchant.get(0).getId())) {
            throw new LSYBusinessException(LSYExceptionCode.DUPLICATE_MERCHANT_PHONE);
        }

        merchant.setId(merchantId);
        if(businessLicenseStatus == 1) {
            merchant.setBusinessLicense("http://47.108.60.93/ossImg/" + "adminPicture" + merchant.getBusinessLicense().substring(36));
        }
        if(tradeLicenseStatus == 1) {
            merchant.setTradeLicense("http://47.108.60.93/ossImg/" + "adminPicture" + merchant.getTradeLicense().substring(36));
        }
        if(idCardFrontStatus == 1) {
            merchant.setIdCardFront("http://47.108.60.93/ossImg/" + "adminPicture" + merchant.getIdCardFront().substring(36));
        }
        if(idCardBackStatus == 1) {
            merchant.setIdCardBack("http://47.108.60.93/ossImg/" + "adminPicture" + merchant.getIdCardBack().substring(36));
        }
        merchantMapper.updateByPrimaryKeySelective(merchant);

        if(businessLicenseStatus == 1) {
            OSSUtil.delete(originalMerchant.getBusinessLicense().substring(27));
            OSSUtil.moveFile("temporary" + merchant.getBusinessLicense().substring(39), "adminPict" + merchant.getBusinessLicense().substring(36));
        }

        if(tradeLicenseStatus == 1) {
            OSSUtil.delete(originalMerchant.getTradeLicense().substring(27));
            OSSUtil.moveFile("temporary" + merchant.getTradeLicense().substring(39), "adminPict" + merchant.getTradeLicense().substring(36));
        }

        if(idCardFrontStatus == 1) {
            OSSUtil.delete(originalMerchant.getIdCardFront().substring(27));
            OSSUtil.moveFile("temporary" + merchant.getIdCardFront().substring(39), "adminPict" + merchant.getIdCardFront().substring(36));
        }

        if(idCardBackStatus == 1) {
            OSSUtil.delete(originalMerchant.getIdCardBack().substring(27));
            OSSUtil.moveFile("temporary" + merchant.getIdCardBack().substring(39), "adminPict" + merchant.getIdCardBack().substring(36));
        }
    }

    /**
     * 临时的ThreadLocal
     */
    private void temporaryThreadLocal() {
        Map<String, Object> map = new HashMap<>();
        map.put("storeId", 1L);
        map.put("merchantId", 1L);
        map.put("sellerId", 1L);
        map.put("sellerPhone", "13228145016");
        ThreadLocalUtil.setObject(JsonUtil.objectToString(map));
    }
}
